<template>
  <el-container>
    <el-header id="header" class="navbar navbar-inverse navbar-fixed-top">
      <nav class="container">
        <div class="navbar-collapse">
          <a href="/">
            <div class="navbar-brand">Submit</div>
          </a>
          <el-menu
            :default-active="activeIndex2"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
            text-color="#fff"
            active-text-color="#e74c3c"
            background-color="#3d4450"
          >
            <el-submenu index="1">
              <template slot="title">任务管理</template>
              <el-menu-item index="1-1">任务列表</el-menu-item>
            </el-submenu>

            <el-submenu index="2">
              <template slot="title">关于submit</template>
              <el-menu-item index="4-1">组员简介</el-menu-item>
            </el-submenu>
          </el-menu>
        </div>
      </nav>
    </el-header>
    <el-main id="boder">
      <div class="container">
        <div class="row">
          <el-col id="mainstay" :span="8">
            <div class="ct">
              <div
                class="status hidden-xs"
                :class="[task.status === '已提交' ? 'bstatus' : 'rstatus']"
              >
                <span class="up">
                  {{ task.up }}
                </span>
                <span class="down">
                  {{ task.down }}
                </span>
              </div>
              <div
                class="mn hidden-xs"
                style="margin-top: 20px; background: #fff"
              >
                <el-row>
                  <el-col :span="21" class="title">
                    <h1>
                      <a href="">{{ task.name }}</a>
                    </h1>
                  </el-col>
                </el-row>
                <el-divider></el-divider>
                <el-row>
                  <el-col :span="21" class="description">
                    <span>任务描述: </span> <br />
                    {{ task.desc }}
                  </el-col>
                </el-row>

                <el-row>
                  <el-col :span="21" class="word">
                    <i class="el-icon-download"></i> <span>模板文档: </span>
                    <br />

                    <span
                    v-for="(item, index) in task.taskWords"
                    :key="item"
                    :offset="index"
                  >
                    <a
                      href=""
                      @click.prevent="fileDownload(item.word)"
                      style="margin-left: 15px"
                      class="link"
                      >{{ item.word }}</a
                    > </br>
                  </span>
                    
                  </el-col>
                </el-row>

                <el-row>
                  <el-col :span="21" class="word">
                    <i class="el-icon-download"></i> <span>已上交的文档: </span>
                    <br />

                    <span
                    v-for="(item, index) in task.subWords"
                    :key="item"
                    :offset="index"
                  >
                    <a
                      href=""
                      @click.prevent="fileDownload(item.word)"
                      style="margin-left: 15px"
                      class="link"
                      >{{ item.word }}</a
                    > </br>
                  </span>
                    
                  </el-col>
                </el-row>

                <el-row>
                  <el-col :span="21" class="upload">
                    <el-upload
                      class="upload-demo"
                      ref="upload"
                      action="http://localhost:8080/sub/upload"
                      accept=".doc,.docx"
                      :on-preview="handlePreview"
                      :on-change="handleChange"
                      :on-remove="handleRemove"
                      :on-exceed="handleExceed"
                      :file-list="fileList"
                      :http-request="uploadFile"
                      :auto-upload="false"
                    >
                      <div class="upload-box">
                        <i class="el-icon-upload"></i>
                        <a slot="trigger">选取文件</a>
                      </div>
                    </el-upload>

                    
                  </el-col>
                  <el-col :span="21" class="upload">
                    <div class="upload-box">
                        <i class="el-icon-upload"></i>
                        <a @click.prevent="submit">确认提交</a>
                    </div>
                  </el-col>
                  
                </el-row>
              </div>
            </div>
          </el-col>

          <el-col id="sidebar" :span="4">
            <aside id="task-login">
              <div class="panel task-panel">
                <div class="panel-heading">
                  <i class="el-icon-sort-up"></i>
                  欢迎!
                  <i class="el-icon-sort-down"></i>
                </div>
                <div class="login-panel text-center">
                  <img
                    src="https://cache.cswsadlab.com/wp-content/uploads/2015/03/笑颜1-150x150.jpg"
                    alt="头像"
                    class="avatar avatar-60 wp-user-avatar wp-user-avatar-60 photo avatar-default"
                    width="60"
                    height="60"
                  />
                  <a class="user-name" href="https://vcb-s.com/wp-admin/"
                    >kamike</a
                  >
                  <a
                    class="btn btn-inverse-primary"
                    href="/login"
                    @click.prevent="logout"
                    title="退出登录"
                    >退出登录</a
                  >
                </div>
              </div>
            </aside>
          </el-col>
        </div>
      </div>
    </el-main>
    <el-footer id="task-footer">
      <div class="container">
        我们的目标是征服星河大海 Powered By
        <a href="/test" target="_blank">Submit</a>
      </div>
    </el-footer>
  </el-container>
</template>

<script>
import { subLokTask } from "@/api/brand.js";
import { subFile } from "@/api/brand.js";
import { downTaskFile } from "@/api/file.js";
import { getToken } from "@/utils/auth";

const defaultSubTask = {
  sid: null,
  tid: null,
  filename: null,
};

export default {
  data() {
    return {
      activeIndex: "1",
      activeIndex2: "1",
      items: [],
      fileList: [],
      subTask: Object.assign({}, defaultSubTask),
      task: {
        name: null,
        desc: null,
        word: null,
        status: null,
        url: null,
      },
      sid: null,
      tid: null,
      name: null,
      headers: {},
    };
  },

  created() {
    this.headers.Authorization = getToken();
    this.getTask();
  },
  methods: {
    logout() {
      this.$store.dispatch("LogOut").then((response) => {});
      this.$router.push({
        path: "/login",
      });
    },
    fileDownload(filename) {
      let params = new URLSearchParams();
      params.append("filename", filename);
      downTaskFile(params).then((response) => {
        const blob = new Blob([response], { type: "text/pain;charset=utf-8" });
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.download = filename;
        a.href = url;
        a.click();
        window.URL.revokeObjectURL(url);
      });
    },
    quit() {
      this.$router.push({
        path: "/login",
      });
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
      if (keyPath[1] === "1-1") {
        this.$router.push({
          path: "/subtask",
          query: { sid: this.sid },
        });
      }
    },
    uploadFile(file) {
      console.log(file.file);
      this.fileData.append("words", file.file);
    },

    submit() {
      console.log(this.task.tstatus);
      if(!(this.task.tstatus === '进行中')){
        this.$message.error("该任务" + this.task.tstatus + ",您无法提交文档");
        return;
      }
      if (this.fileList.length === 0) {
        this.$message({
          message: "请先选择文件",
          type: "warning",
        });
      } else {
        const isLt10M = this.fileList.every(
          (file) => file.size / 1024 / 1024 < 10
        );
        if (!isLt10M) {
          this.$message.error("请检查，上传文件大小不能超过10MB!");
        } 
        
        else {
          this.fileData = new FormData();
          this.$refs.upload.submit();
          this.fileData.append("sid", this.sid);
          this.fileData.append("tid", this.tid);
          subFile(this.fileData).then((response) => {});
        }
      }
    },
    //移除
    handleRemove(file, fileList) {
      this.fileList = fileList;
      // return this.$confirm(`确定移除 ${ file.name }？`);
    },

    // 选取文件超过数量提示
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },

    //监控上传文件列表
    handleChange(file, fileList) {
      let existFile = fileList
        .slice(0, fileList.length - 1)
        .find((f) => f.name === file.name);
      if (existFile) {
        this.$message.error("当前文件已经存在!");
        fileList.pop();
      }
      this.fileList = fileList;
    },

    getTask() {
      this.sid = this.$route.query.sid;
      this.tid = this.$route.query.tid;
      subLokTask(this.sid, this.tid).then((response) => {
        let data = response.data;
        this.task.name = data.task.title;
        this.task.desc = data.task.description.replace("/\r\n/g", " <br/> ");
        this.task.taskWords = data.task.words;
        this.task.subWords = data.words;
        this.task.tstatus = this.getTaskStatus(data.task.status);
        this.task.status = data.status === 0 ? "未提交" : "已提交";
        this.task.up = this.task.status.substring(0, 2);
        this.task.down = this.task.status.substring(2, 3);
      });
    },
    getTaskStatus(status) {
      var name = "";
      switch (status) {
        case 0:
          name = "未开始";
          break;
        case 1:
          name = "运行中";
          break;
        case 2:
          name = "已结束";
          break;
        case 3:
          name = "暂停中";
          break;
      }
      return name;
    },
    onSubmit() {
      console.log("submit!");
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
  },
};
</script>

<style scoped>
.status .up {
  margin-top: 10px;
  text-align: center;
  display: block;
}

.status .down {
  font-size: 30px;
  font-weight: bold;
  margin-top: -5px;
  text-align: center;
  display: block;
}

.status {
  position: absolute;
  height: 70px;
  width: 70px;
  border-radius: 100px;
  left: -20px;
  top: -20px;
  box-shadow: 1px 2px 7px rgb(0, 0, 0);
}

.bstatus {
  background-color: #d9edf7;
  border-color: #d9edf7;
  color: #000;
}

.rstatus {
  background-color: #e74c3c;
  border-color: #e74c3c;
  color: #fff;
}

.hidden-xs {
  display: block;
}
.upload {
  padding: 15px;
  margin-bottom: 20px;
  border: 1px solid transparent;
  border-top-color: transparent;
  border-right-color: transparent;
  border-bottom-color: transparent;
  border-left-color: transparent;
  border-radius: 4px;
  overflow: auto;
  color: #3a87ad;
  background-color: #d9edf7;
  border-color: #bce8f1;
  margin: 10px auto;
  text-align: center;
  font-size: 14px;
}
.link {
  font-size: 12px;
  color: #d2322d;
  padding: 5px;
}
.word {
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  border: 1px solid;
  border-top-color: currentcolor;
  border-right-color: currentcolor;
  border-bottom-color: currentcolor;
  border-left-color: currentcolor;
  clear: both;
  margin: 15px 0;
  padding: 15px 20px 15px 30px;
  background: #d9edf7;
  border-color: #bce8f1;
  color: #3a87ad;
}
.description {
  color: #666666;
  background-color: #ecf0f1;
  border: 1px solid #ecf0f1;
  padding: 15px;
  margin-bottom: 20px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: auto;
}
.title {
  padding: 20px 15px 0px 15px;
}

.title h1 {
  font-size: 32px;
  margin: 0 auto;
  text-align: center;
}

.title h1 a {
  color: #3d4450;
}
#task-footer {
  display: block;
  text-align: center;
  color: #ecf0f1;
  padding: 50px;
  background-color: #3d4450;
}
#task-footer a {
  color: #ecf0f1;
}

#task-footer {
  display: block;
  text-align: center;
  color: #ecf0f1;
  padding: 50px;
  background-color: #3d4450;
}
#task-footer a {
  color: #ecf0f1;
}

#sidebar aside > div {
  border-radius: 4px;
  box-shadow: 1px -1px 6px rgba(155, 89, 182, 0.5);
  -webkit-box-shadow: 1px -1px 6px rgba(155, 89, 182, 0.5);
  background-color: white;
}

.panel {
  margin-bottom: 20px;
  border: 1px solid transparent;
}

.panel-heading {
  border-bottom: none;
  font-size: 14px;
  line-height: 14px;
  font-weight: normal;
}
.panel-heading {
  padding: 10px 15px;
  border-bottom: 1px solid transparent;
  border-top-right-radius: 3px;
  border-top-left-radius: 3px;
}
.login-panel {
  height: 100px;
  padding: 20px 0px;
}
.text-center {
  text-align: center;
}
.login-panel img {
  margin-right: 20px;
  border-radius: 50%;
  border: 2px solid white;
  background-color: white;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
img {
  max-width: 100%;
  border-radius: 4px;
  vertical-align: middle;
  height: auto;
}
.login-panel .user-name {
  margin-right: 20px;
}
.btn-inverse-primary {
  color: white !important;
  background-color: #3d4450;
  border-color: #3d4450;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
.task-panel a {
  color: #666666;
}
.btn {
  display: inline-block;
  padding: 6px 12px;
  margin-bottom: 0;
  font-size: 12px;
  font-weight: normal;
  line-height: 1.428571429;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}
.btn-danger {
  color: #ffffff;
  background-color: #e74c3c;
  border-color: #e74c3c;
  transition: all 0.25s ease-in-out;
  -webkit-transition: all 0.25s ease-in-out;
  -moz-transition: all 0.25s ease-in-out;
  -o-transition: all 0.25s ease-in-out;
}
.el-header {
  background-color: #3d4450;
  border-color: #080808;
  top: 0;
  z-index: 1030;
}
#header {
  display: fixed;
  padding: 0;
  min-height: 50px;
}

.container {
  max-width: 1170px;
  margin-right: auto;
  margin-left: auto;
}

.el-menu {
  background-color: #3d4450;
  font-size: 14px;
  display: block;
}

.el-menu::after,
.el-menu::before {
  display: none;
}

.navbar-brand {
  margin-left: -15px;
  width: 200px;
  height: 50px;
  display: block;
  float: left;
  font-size: 40px;
  color: white;
  text-align: center;
}

.navbar-collapse {
  margin-left: -15px;
  margin-right: -15px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

#boder {
  padding: 30px 0;
}

.container {
  padding-left: 30px;
  padding-right: 20px;
}

.row {
  margin-right: -15px;
  margin-left: -15px;
}

#mainstay {
  width: 66.666666666666%;
  float: left;
  padding-right: 15px;
  padding-left: 15px;
}

#sidebar {
  width: 33.333333333333%;
}
body {
  word-wrap: break-word;
}
body {
  font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;
  font-size: 12px;
  line-height: 1.428571429;
  color: #3d4450;
}
a {
  text-decoration: none;
}

.ct {
  border-radius: 4px;
  box-shadow: -1px -1px 10px rgba(155, 89, 182, 0.5);
  -webkit-box-shadow: -1px -1px 10px rgba(155, 89, 182, 0.5);
  position: relative;
  margin-bottom: 30px;
  padding-bottom: 20px;
  min-height: 20px;
  padding: 19px;
  padding-bottom: 19px;
  margin-bottom: 30px;
  border-radius: 6px;
  background-color: #ffffff;
  -webkit-box-shadow: 0px 0px 10px #9b59b6;
  -moz-box-shadow: 0px 0px 10px #9b59b6;
  box-shadow: 0px 0px 10px #9b59b6;
}

.crinfo-btn {
  text-align: center;
}
</style>